//
//  keywordViewController.m
//  Qya
//
//  Created by maxwellpang on 12-9-7.
//  Copyright (c) 2012年 __MyCompanyName__. All rights reserved.
//

#import "keywordViewController.h"
#import "FeedsSourceViewController.h"
#import "KWFieldViewController.h"

#define EDGE_X 5
#define EDGE_Y 2
#define FIELDVIEW_SIZE_WIDTH 95
#define FIELDVIEW_SIZE_HEIGHT 40

@interface keywordViewController ()

@end

@implementation keywordViewController

static keywordViewController *sharedKeyWordViewController;

@synthesize mKeyWordArray;
@synthesize mSearchArray;
@synthesize backgroundImageView;

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    if (sharedKeyWordViewController == nil)
    {
        self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
        if (self) {
            // Custom initialization
            stage = 0;
            mKeyWordArray = [[NSMutableArray alloc] init];
            mSearchArray = [[NSMutableArray alloc] initWithObjects:@"咖啡",@"打篮球",@"iphone5", nil];
            wholeWord = [[NSString alloc]initWithString:@""];
            //        [mKeyWordArray addObject:@"360"];
            //        NSLog(@"%d", [self.navigationController.viewControllers count]);
        }
        sharedKeyWordViewController = self;
        return self;
    }
    else
    {
        return sharedKeyWordViewController;
    }
}

+ (id)sharedKeyWordViewController
{
    return sharedKeyWordViewController;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    
    CGContextRef context = UIGraphicsGetCurrentContext();
    [UIView beginAnimations:@"moveDown" context:context];
    [UIView setAnimationDuration:1.0];
    
    [self.backgroundImageView setAlpha:0.2f];
    
    [UIView commitAnimations];

    [self.navigationController.navigationBar setBackgroundImage:[UIImage imageNamed:@"dingbubar"] forBarMetrics:0]; 
    [self.navigationController.toolbar setBackgroundImage:[UIImage imageNamed:@"dibubar"] forToolbarPosition:UIToolbarPositionBottom barMetrics:0];
     
    UIBarButtonItem *rightButton = [[UIBarButtonItem alloc] initWithTitle:@"跳过" style:UIBarButtonItemStylePlain target:self action:@selector(selectConfirmAction:)];  
    self.navigationItem.rightBarButtonItem = rightButton;
    self.navigationItem.hidesBackButton = YES;
    
    CGRect itframe = [inputToolView frame];
    itframe.origin.y += 129; 
    [inputToolView setFrame:itframe];
    [self.view addSubview:inputToolView];
    input.delegate = self;
    [input becomeFirstResponder];
}

- (void)viewDidUnload
{
    [super viewDidUnload];
    // Release any retained subviews of the main view.
    // e.g. self.myOutlet = nil;
}

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
    return (interfaceOrientation == UIInterfaceOrientationPortrait);
}

-(void)selectConfirmAction:(id)sender  
{  
    if (stage == 0) {
        //按钮为跳过
        FeedsSourceViewController *secondView = [[FeedsSourceViewController alloc] init];  
        [self.navigationController pushViewController:secondView animated:YES];  
        secondView.title = @"信息来源"; 
    }
    else {
        //按钮为确定
        FeedsSourceViewController *secondView = [[FeedsSourceViewController alloc] init];  
        [self.navigationController pushViewController:secondView animated:YES];  
        secondView.title = @"信息来源"; 
        stage = 0;
        [mSearchArray addObject:wholeWord];
    }

}  

-(IBAction)addKeyword:(id)sender
{
    
    if ([[input text] isEqualToString:@""]) {
        return;
    }
    [self.navigationItem.rightBarButtonItem setTitle:@"确定"];
    stage = 1;
//    [input resignFirstResponder];

    
//    [UIView beginAnimations:@"turnDark" context:nil];
//    [UIView setAnimationDelegate:self];
//    [UIView setAnimationDuration:0.3];
//    
//    CGPoint itCenter = [inputToolView center];
//    itCenter.y += 215; 
//    inputToolView.center = itCenter;
//    
//    [UIView commitAnimations];
    
    [mKeyWordArray addObject:[input text]];

    if ([wholeWord isEqualToString:@""]) {
        wholeWord =[input text];
    }

    [input setText:@""]; 
    
    [self showKeyWord];
}

- (void)showKeyWord
{
    backgroundImageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"guanjiancidiban"]];
    CGRect backgroundImageViewRect = CGRectMake(5, 5, self.view.frame.size.width-10, 109);
    [backgroundImageView setFrame:backgroundImageViewRect];
    [self.view addSubview:backgroundImageView];
    
    for (int i=0; i<[mKeyWordArray count]; ++i)
    {
        NSInteger row, colum;
        row = i / 3;
        colum = i % 3;
        
        NSInteger x, y;
        if (colum == 0)
            x = EDGE_X;
        else if (colum == 1)
            x = 2 * EDGE_X + FIELDVIEW_SIZE_WIDTH;
        else
            x = 3 * EDGE_X + 2 * FIELDVIEW_SIZE_WIDTH;
        y = EDGE_Y + row * (FIELDVIEW_SIZE_HEIGHT + EDGE_Y);
        
        CGRect rect = CGRectMake(x, y, FIELDVIEW_SIZE_WIDTH, FIELDVIEW_SIZE_HEIGHT);
        KWFieldViewController *kwFieldViewController = [[KWFieldViewController alloc] initWithNibName:@"KWFieldViewController" bundle:nil];
        [kwFieldViewController.view setFrame:rect];
        [backgroundImageView addSubview:kwFieldViewController.view];
        kwFieldViewController.mKeyWord.text = [mKeyWordArray objectAtIndex:i];
    }
}

//- (BOOL)textFieldShouldBeginEditing:(UITextField *)textField
//{
//    NSLog(@"   begin editing ");
//    [UIView beginAnimations:@"turnDark" context:nil];
//    [UIView setAnimationDelegate:self];
//    [UIView setAnimationDuration:0.3];
//    
//    CGPoint itCenter = [inputToolView center];
//    itCenter.y -= 215; 
//    inputToolView.center = itCenter;
//    
//    [UIView commitAnimations];
//    return YES;
//}

//- (BOOL)textFieldShouldEndEditing:(UITextField *)textField
//{
//    NSLog(@"   end editing ");
//    [inputToolView resignFirstResponder];
//    //    itframe.origin.y += 370;
//    CGPoint itCenter = [inputToolView center];
//    itCenter.y += 300; 
//    inputToolView.center = itCenter;
//    //    [postScrollView setContentOffset:CGPointMake(0, 150) animated:YES];
//    return YES;
//}




@end
